package com.example.financeapi.controller;

import com.example.financeapi.config.dynamicquery.DynamicQuery;
import com.example.financeapi.utils.JsoupUtils;
import com.example.financeapi.entity.vo.ResultVOUtil;
import com.example.financeapi.entity.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 爬虫测试
 */
@RestController
@RequestMapping("/crawl")
public class CrawlController {

    @Autowired
    private DynamicQuery dynamicQuery;

    /**
     * 爬取-爪哇笔记
     * 1）获取数据库配置
     * 2）根据数据库配置读取文章相关元素
     * 3）富文本转MD，并返回前端实体内容
     * 源码：https://gitee.com/52itstyle/SPTools
     * @param url
     * @return
     */
    @RequestMapping("crawlTest")
    public ResultVo crawl(String url) {
        try {
            String domain = JsoupUtils.getDomain(url);
            String nativeSql = "SELECT * FROM app_blog_crawl WHERE url = ?";
/*            AppBlogCrawl crawl = dynamicQuery.nativeQuerySingleResult(AppBlogCrawl.class,nativeSql,new Object[]{domain});
            if(crawl!=null){
                Document document = JsoupUtils.getDocument(url);
                String title = document.select(crawl.getTitle()).text();
                String content = document.select(crawl.getContent()).html();
                Remark remark = new Remark();
                String markdown = remark.convertFragment(content);
                AppBlog blog = new AppBlog();
                blog.setTitle(title);
                blog.setContent(markdown);
                blog.setUrl(url);
                return ResultVOUtil.success(blog);
            }else{
                return ResultVOUtil.error("目前暂不支持此网站抓取");
            }*/
        } catch (Exception e) {
            return ResultVOUtil.error("抓取异常");
        }
        return ResultVOUtil.error("抓取异常");
    }
}
